const { Op } = require('sequelize')
let ingoreAttr = ['pageNum', 'pageSize', 'sortBy'] // 忽略判断对象的属性
// 循环对象判断是否存在
function blurSelect (obj) {
  let conditions = {}
  // 说明传入的不是空对象
  if (Object.keys(obj).length !== 0) {
    for (const attr in obj) {
      // attr 属性不在忽略的list中
      if (ingoreAttr.indexOf(attr) === -1) {
        if (attr === 'id') {
          obj[attr] ? conditions['_id'] = obj[attr] : null
        } else if (attr === 'cTime' || attr === 'uTime') {
          let arr = obj[attr].split('-')
          if (arr.length === 3) {
            let year = parseInt(arr[0], 10) + 2000 // 将年份转换为四位数形式
            let month = parseInt(arr[1], 10)
            let day = parseInt(arr[2], 10)
            let startDateTime = new Date(year, month - 1, day) // 注意月份需要减1，因为 JavaScript 的月份是从 0 开始的
            let endDateTime = new Date(year, month - 1, day + 1) // 结束时间为当前日期的下一天
            conditions[attr] = {
              [Op.gte]: startDateTime,
              [Op.lt]: endDateTime
            }
          }
        } else {
          if (obj[attr]) {
            if (obj[attr].length === 24) {
              conditions[attr] = obj[attr]
            } else {
              let reg = new RegExp(obj[attr], 'i')
              conditions[attr] = {
                [Op.like]: reg
              };
            }
          }
        }
      }
    }
  }
  return conditions
}
// 循环对象判断属性是否存在 精准匹配
function completeSelect (obj) {
  let condition = {}
  if (JSON.stringify(obj) !== '{}') {
    for (const attr in obj) {
      if (!ingoreAttr.includes(attr)) {
        if (obj[attr] || obj[attr] === 0) {
          condition[attr] = obj[attr];
        }
      }
    }
  }
  return condition
}
// 返回分页查询对象
function pageSelect (obj) {
  let pageSize = Math.max(parseInt(obj.pageSize), 1) || 10  // 分页大小
  let currentPage = Math.max(parseInt(obj.pageNum), 1) || 1
  let pageObj = {
    limit: pageSize, // 分页大小
    skip: (currentPage - 1) * pageSize // 查询的起始位置
  }
  if (obj.sortBy) {
    let sortBy = obj.sortBy
    let sortOrders = obj.sortOrders
    let order = 'desc'
    if (sortOrders === '1' || sortOrders === 1 || sortOrders === 'asc' || sortOrders === 'ascending') {
      order = 'asc'
    }
    pageObj.sort = {
      [sortBy]: order
    }
  }
  return pageObj
}
function listSelect (obj) {
  let condition = {}
  if (JSON.stringify(obj) !== '{}') {
    for (const attr in obj) {
      if (!ingoreAttr.includes(attr)) {
        if (obj[attr] !== '') {
          if (attr.indexOf('name') > -1 || attr.indexOf('from') > -1) {
            condition[attr] = {
              [Op.like]: `%${ obj[attr] }%`
            }
          } else {
            condition[attr] = obj[attr]
          }
        }
      }
    }
  }
  return condition
}
/**
 * 应答报文
 * @param {*} msg 
 * @param {*} code 
 * @param {*} data 
 */
function responseContent (res, msg, code, data = {}) {
  let response = {}
  response['msg'] = msg
  response['code'] = code
  response['data'] = data
  return res.status(code).json(response)
}
function resContent (status, msg, code) {
  let response = {}
  response['msg'] = msg
  response['code'] = code
  return res.status(status).json(response)
}

/**
 * 返回顶部的通用方法
 * @param {*} btnId 原生DOM
 */
function backTop (btnId) {
  var btn = document.getElementById(btnId);
  var d = document.documentElement;
  var b = document.body;
  window.onscroll = set;
  btn.style.display = "none";
  btn.onclick = function () {
    btn.style.display = "none";
    window.onscroll = null;
    this.timer = setInterval(function () {
      d.scrollTop -= Math.ceil((d.scrollTop + b.scrollTop) * 0.1);
      b.scrollTop -= Math.ceil((d.scrollTop + b.scrollTop) * 0.1);
      if ((d.scrollTop + b.scrollTop) == 0) clearInterval(btn.timer, window.onscroll = set);
    }, 10);
  };
  function set () {
    btn.style.display = (d.scrollTop + b.scrollTop > 100) ? 'block' : "none"
  }
}
/**
 * 实现base64 解码
 * @param {*} data 
 * @returns 
 */
function base64_decode (data) {
  var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  var o1, o2, o3, h1, h2, h3, h4, bits, i = 0, ac = 0, dec = "", tmp_arr = [];
  if (!data) { return data; }
  data += '';
  do {
    h1 = b64.indexOf(data.charAt(i++));
    h2 = b64.indexOf(data.charAt(i++));
    h3 = b64.indexOf(data.charAt(i++));
    h4 = b64.indexOf(data.charAt(i++));
    bits = h1 << 18 | h2 << 12 | h3 << 6 | h4;
    o1 = bits >> 16 & 0xff;
    o2 = bits >> 8 & 0xff;
    o3 = bits & 0xff;
    if (h3 == 64) {
      tmp_arr[ac++] = String.fromCharCode(o1);
    } else if (h4 == 64) {
      tmp_arr[ac++] = String.fromCharCode(o1, o2);
    } else {
      tmp_arr[ac++] = String.fromCharCode(o1, o2, o3);
    }
  } while (i < data.length);
  dec = tmp_arr.join('');
  dec = utf8_decode(dec);
  return dec
}

/**
 * 实现utf-8解码
 * @param {*} str_data 
 * @returns 
 */
function utf8_decode (str_data) {
  var tmp_arr = [], i = 0, ac = 0, c1 = 0, c2 = 0, c3 = 0; str_data += '';
  while (i < str_data.length) {
    c1 = str_data.charCodeAt(i);
    if (c1 < 128) {
      tmp_arr[ac++] = String.fromCharCode(c1);
      i++;
    } else if (c1 > 191 && c1 < 224) {
      c2 = str_data.charCodeAt(i + 1);
      tmp_arr[ac++] = String.fromCharCode(((c1 & 31) << 6) | (c2 & 63));
      i += 2;
    } else {
      c2 = str_data.charCodeAt(i + 1);
      c3 = str_data.charCodeAt(i + 2);
      tmp_arr[ac++] = String.fromCharCode(((c1 & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
      i += 3;
    }
  }
  return tmp_arr.join('');
}
var MAX_SAFE_INTEGER = 9007199254740991
/**
 * 用于判断一个值是否符合数组或类数组对象的长度要求
 * @param {*} value 
 * @returns 
 */
function isLength (value) {
  return typeof value == 'number' && value > -1 && value % 1 == 0 && value <= MAX_SAFE_INTEGER
}
/**
 * 判断一个值是否是类数组
 * @param {*} value 
 * @returns 
 */
function isArrayLike (value) {
  return value != null && isLength(value.length) && !isFunction(value)
}
/**
 * 判断一个值是否是正则表达式
 * @param {*} value 
 * @returns 
 */
function isRegExp (value) {
  return Object.prototype.toString.call(value).slice(8, -1) === 'RegExp'
}
/**
 * 判断一个值是否为数组类型
 * @param {*} value 
 * @returns 
 */
function isArray (value) {
  // return Array.isArray(value)
  // return Object.prototype.toString.call(value) === '[object Array]'
  return Object.prototype.toString.call(value).slice(8, -1) === 'Array'
}
/**
 * 判断一个值是否为空
 * @param {*} value 
 * @returns 
 */
function isEmpty (value) {
  if (value === null) {
    return true
  }
  if (isArrayLike(value) && (isArray(value) || typeof value === 'string' || typeof value.splice
    == 'function')) {
    return !value.length
  }
  for (var key in value) {
    if (hasOwnProperty.call(value, key)) {
      return false
    }
  }
  return true
}
/**
 * 判断一个值是否是NaN
 * @param {*} value 
 * @returns 
 */
function isNaN (value) {
  return isNumber(value) && value != +value
}
/**
 * 判断一个值是否等于undefined
 * @param {*} value 
 * @returns 
 */
function isUndefined (value) {
  return value === void 0
}
/**
 * 判断是否为null
 * @param {*} value 
 * @returns 
 */
function isNull (value) {
  return value === null
}
/**
 * 判断是否是Number 类型
 * @param {*} value 
 * @returns 
 */
function isNumber (value) {
  return !isNaN(value) && typeof value === 'number';
}
/**
 * 判断一个值是否是字符串
 * @param {*} value 
 * @returns 
 */
function isString (value) {
  return Object.prototype.toString.call(value) === '[object String]';
}
/**
 * 判断一个值是不是对象
 * @param {*} value 
 * @returns 
 */
function isObject (value) {
  var type = typeof value;
  return value != null && (type == 'object' || type == 'function');
}
function baseGetTag (value) {
  if (value === null) {
    return value === void 0 ? '[object Undefined]' : '[object Null]'
  }
  return Object.prototype.toString.call(value)
}
/**
 * 判断一个值是否是函数
 * @param {*} value 
 * @returns 
 */
function isFunction (value) {
  if (!isObject(value)) {
    return false
  }
  var tag = baseGetTag(value)
  return tag == '[object Function]' || tag == '[object GeneratorFunction]' || tag == '[object AsyncFunction]' || tag == '[object Proxy]'
}

module.exports = {
  responseContent,
  pageSelect,
  completeSelect,
  resContent,
  listSelect,
  blurSelect,
  isLength,
  isUndefined,
  isString,
  isArrayLike,
  isArray,
  isEmpty,
  isRegExp,
  isNull,
  isNumber,
  isObject,
  isFunction
}